//
//  HMVisitorView.swift
//  Weibo12
//
//  Created by HM on 15/12/28.
//  Copyright © 2015年 itheima. All rights reserved.
//

import UIKit
import SnapKit

/// 访客视图的协议
// Swift 中协议必须继承基协议（NSObjectProtocol），否则不能给 delegate 设置为 weak
//protocol HMVisitorViewDelegate: NSObjectProtocol {
//    func registerButtonClick()
//    func loginButtonClick()
//}

class HMVisitorView: UIView {
    
//    weak var delegate: HMVisitorViewDelegate?
    
    // 通过代码创建出来的 view 都会走这个方法，所以外界可以调用  alloc:init
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    /// 供外界设置当前访客视图的内容
    ///
    /// - parameter imgName:     图片名
    /// - parameter messageText: 文字内容
    func setupInfo(imgName: String?, messageText: String?){
        if imgName != nil {
            circleView.hidden = true
            iconView.image = UIImage(named: imgName!)
            messageLabel.text = messageText
        }else{
            // 如果 imgName 为 nil ，就代表首页
            startAnimation()
        }
    }
    
    /// 执行转圈的动画
    private func startAnimation(){
        // 初始化动画
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        // 执行一圈的时候
        anim.duration = 20
        // 转圈
        anim.toValue = 2 * M_PI
        // 重复次数
        anim.repeatCount = MAXFLOAT
        // 切换界面，或者 app 切到桌面上， CABasicAnimation 会被释放
        anim.removedOnCompletion = false
        circleView.layer.addAnimation(anim, forKey: nil)
    }
    
    // MARK: - 监听方法
    
//    @objc private func registerButtoClick(){
//        delegate?.registerButtonClick()
//    }
//    
//    @objc private func loginButtoClick(){
//        delegate?.loginButtonClick()
//    }
    
    // MARK: - 懒加载控件
    
    // 大图标的控件
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    // 圆圈
    private lazy var circleView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    // 消息label
    private lazy var messageLabel: UILabel = {
        let label = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 14)
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        // 换行属性
        label.numberOfLines = 0
        // 文字居中
        label.textAlignment = .Center
        return label
    }()
    
    // 注册按钮
    lazy var registerButton: UIButton = {
        let button = UIButton(textColor: UIColor.orangeColor(), fontSize: 15)
        // 添加点击事件
        // button.addTarget(self, action: "registerButtoClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        button.setTitle("注册", forState: UIControlState.Normal)
        // 设置背景图片
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        return button
    }()
    
    // 登录
    lazy var loginButton: UIButton = {
        let button = UIButton(textColor: UIColor.darkGrayColor(), fontSize: 15)
        // 添加点击事件
        // button.addTarget(self, action: "loginButtoClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        button.setTitle("登录", forState: UIControlState.Normal)
        // 设置背景图片
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        return button
    }()
    
    // 半透明阴影
    private lazy var maskIconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))

}


extension HMVisitorView {
    // 设置视图
    private func setupUI(){
        backgroundColor = UIColor(white: 237/255, alpha: 1)
        
        // 添加控件
        addSubview(circleView)
        addSubview(maskIconView)
        addSubview(iconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        // 添加约束
        // > 1. 大图标
        iconView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(snp_center)
        }
        // 2.> 圆圈
        // @noescape --> 代表后面定义的闭包是在当前代码位置使
        circleView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(iconView.snp_center)
        }
        // 3.> message的label
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(circleView.snp_centerX)
            make.top.equalTo(circleView.snp_bottom).offset(16)
            make.width.equalTo(224)
        }
        // 4.> 注册按钮
        registerButton.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(messageLabel.snp_leading)
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
        }
        // 5.> 登录按钮
        loginButton.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(messageLabel.snp_trailing)
            make.top.equalTo(registerButton.snp_top)
            make.width.equalTo(100)
        }
        // 5.> 阴影
        // VFL: 可视化格式语言
        // | : 边界
        // H : 约束方向
        // metrics : 用于设置格式字符串里面的 `()` 里面的数值
        // views: 用于设置格式字符串里面的 `[]` 里面的View
        
        maskIconView.snp_makeConstraints { (make) -> Void in
            make.width.equalTo(snp_width)
            make.top.equalTo(snp_top)
            make.bottom.equalTo(registerButton.snp_bottom)
        }
//        maskIconView.translatesAutoresizingMaskIntoConstraints = false
//        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[view]-0-|", options: [], metrics: nil, views: ["view" : maskIconView]))
//        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[view]-(offsetY)-[registerButton]", options: [], metrics: ["offsetY": -35], views: ["view": maskIconView, "registerButton": registerButton]))
        
    }
    
    // 设置视图
    private func setupUIDemo(){
        backgroundColor = UIColor(white: 237/255, alpha: 1)
        
        // 添加控件
        addSubview(circleView)
        addSubview(maskIconView)
        addSubview(iconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        // 添加约束
        
        // > 1. 大图标
        iconView.translatesAutoresizingMaskIntoConstraints = false
        
        // 约束水平居中
        /// @param iconView                  要约束的控件
        /// @param NSLayoutAttribute.CenterX 约束哪一个属性
        /// @param NSLayoutRelation.Equal    相等，大于等于，小于等于
        /// @param self                      相对于的控件
        /// @param NSLayoutAttribute.CenterX 约束相对于的控件哪一个属性
        /// @param multiplier                倍数
        /// @param constant                  偏移量
        ///
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        
        // 2.> 圆圈
        circleView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: circleView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: circleView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        // 3.> message的label
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Top, relatedBy: .Equal, toItem: circleView, attribute: .Bottom, multiplier: 1, constant: 16))
        // 约束宽度
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 224))
        
        
        // 4.> 注册按钮
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Leading, relatedBy: .Equal, toItem: messageLabel, attribute: .Leading, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Top, relatedBy: .Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        
        // 5.> 登录按钮
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Trailing, relatedBy: .Equal, toItem: messageLabel, attribute: .Trailing, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Top, relatedBy: .Equal, toItem: registerButton, attribute: .Top, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        
        // 5.> 阴影
        // VFL: 可视化格式语言
        // | : 边界
        // H : 约束方向
        // metrics : 用于设置格式字符串里面的 `()` 里面的数值
        // views: 用于设置格式字符串里面的 `[]` 里面的View
        maskIconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[view]-0-|", options: [], metrics: nil, views: ["view" : maskIconView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[view]-(offsetY)-[registerButton]", options: [], metrics: ["offsetY": -35], views: ["view": maskIconView, "registerButton": registerButton]))
        
    }
}
